package com.ddwl.common.util;

import com.ddwl.schema.util.DateUtil;

import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author Fu Zhaohui
 */
public final class UUIDGenerator {

    private static final String baseDigits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final int BASE = baseDigits.length();
    private static final char[] digitsChar = baseDigits.toCharArray();
    private static AtomicLong index = new AtomicLong();

    private static SnowflakeIdGenerator snowflakeIdGenerator;

    public static SnowflakeIdGenerator getSnowflakeIdGenerator(){
        if (null == snowflakeIdGenerator) {
            snowflakeIdGenerator=SpringBeanUtil.getBean(SnowflakeIdGenerator.class);
        }
        return snowflakeIdGenerator;
    }

    public final static String getUUID() {
        String s = UUID.randomUUID().toString();
        return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
    }

    public final static String getTimeUUID() {
        String workId = String.valueOf(getSnowflakeIdGenerator().getWorkId() % 100);
        return DateUtil.getCurrDate(DateUtil.DATE_TIME_FORMAT4) + workId + String.format("%03d", index.getAndIncrement() % 1000);
    }

    public final static String getTimeUUID1() {
        return System.currentTimeMillis() + String.format("%03d", index.getAndIncrement() % 1000);
    }

    public final static String getRandomCode() {
        return (Math.random() + "").substring(2);
    }

    public final static String[] getUUID(int number) {
        if (number < 1) {
            return null;
        }
        String[] ss = new String[number];
        for (int i = 0; i < number; i++) {
            ss[i] = getUUID();
        }
        return ss;
    }

    public final static String getShortTimeUUID() {
        return Long.toHexString(Long.parseLong(getTimeUUID())).toUpperCase();
    }

    public static String getShortUUID() {
        return getSnowflakeIdGenerator().nextIdToString();
    }

    private static String getOldShortUUID(){
        long number = System.currentTimeMillis() * 1000000 + Long.parseLong(RandomGenerateUtil.getRandomS(1, 6));
        StringBuilder buf = new StringBuilder();
        while (number != 0) {
            buf.append(digitsChar[(int) (number % BASE)]);
            number /= BASE;
        }
        return buf.reverse().toString();
    }

    public static String getShortUUID(long number) {
        StringBuilder buf = new StringBuilder();
        while (number != 0) {
            buf.append(digitsChar[(int) (number % BASE)]);
            number /= BASE;
        }
        return buf.reverse().toString();
    }

    public static long reverseShortUUID(String uuid) {
        char[] chars = uuid.toCharArray();
        long j = 0;
        for (int i = 0; i < chars.length;i++) {
            j += baseDigits.indexOf(chars[i])*mi(BASE,Long.valueOf(chars.length-i-1));
        }
        return j;
    }

    public static Long mi(long i,long j){
        Long mi = 1L;
        while (j>0){
            j--;
            mi = mi*i;
        }
        return mi;
    }

    /**
     * 获取六位随机数
     *
     * @return
     */
    public static String getShortRandom() {
        long number = System.currentTimeMillis() / 1000;
        StringBuilder buf = new StringBuilder();
        while (number != 0) {
            buf.append(digitsChar[(int) (number % BASE)]);
            number /= BASE;
        }
        return buf.reverse().toString();
    }
}
